import os
from rich import print
from cryptography.fernet import Fernet
from .common import process_directory

def encrypt(args):
    if not os.path.exists(args.key):
        print(f'[bold red]密钥文件 {args.key} 不存在[/]')
        return
    with open(args.key, 'rb') as key_file:
        key_data = key_file.read()
    cipher = Fernet(key_data)
    process_directory(args.path, cipher, 'encrypt')

def decrypt(args):
    if not os.path.exists(args.key):
        print(f'[bold red]密钥文件 {args.key} 不存在[/]')
        return
    with open(args.key, 'rb') as key_file:
        key_data = key_file.read()
    cipher = Fernet(key_data)
    process_directory(args.path, cipher, 'decrypt')

def genkey(args):
    key = Fernet.generate_key()
    with open(args.key, 'wb') as key_file:
        key_file.write(key)
    print(f'加密密钥已保存至: [bold cyan]`{args.key}`[/]')
